package com.alibaba.citrus.cr.platform.enhance.repository.impl;

import com.alibaba.citrus.cr.platform.enhance.repository.CustomerAddressRelationRepository;
import com.alibaba.cz.base.tool.Nullable;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crplatformenhance.customeraddressrelation.dto.CreateResponse;
import com.epoch.app.crplatformenhance.customeraddressrelation.dto.DeleteRequest;
import com.epoch.app.crplatformenhance.customeraddressrelation.dto.DeleteResponse;
import com.epoch.app.crplatformenhance.customeraddressrelation.model.CustomerAddressRelation;
import com.epoch.app.crplatformenhance.customeraddressrelation.service.CustomerAddressRelationService;
import com.epoch.app.crplatformenhance.model.dto.CustomerAddressRelationLoadListRequest;
import com.epoch.app.crplatformenhance.model.dto.CustomerAddressRelationSDO;
import com.epoch.app.crplatformenhance.model.enums.CustomerRelationBizTypeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class CustomerAddressRelationRepositoryImpl implements CustomerAddressRelationRepository {
    private final Log log = Log.getLogger(CustomerAddressRelationRepositoryImpl.class);

    @Resource
    private CustomerAddressRelationService customerAddressRelationService;

    @Override
    public Boolean createAddressRelation(CustomerAddressRelationSDO sdo) {
        CustomerAddressRelation relation = new CustomerAddressRelation();
        BeanUtils.copyProperties(sdo,relation);
        relation.setBizType(CustomerRelationBizTypeEnum.ADDRESS_CUSTOMER_PRINCIPAL_RELATION.getValue());
        CreateResponse response = customerAddressRelationService.create(relation);
        return response!=null && response.getLastInsertId()!=null;
    }

    @Override
    public Boolean createSaleScopeRelation(CustomerAddressRelationSDO sdo) {
        CustomerAddressRelation relation = new CustomerAddressRelation();
        BeanUtils.copyProperties(sdo,relation);
        relation.setBizType(CustomerRelationBizTypeEnum.SALESCOPE_CUSTOMER_PRINCIPAL_RELATION.getValue());
        CreateResponse response = customerAddressRelationService.create(relation);
        return response!=null && response.getLastInsertId()!=null;
    }

    @Override
    public Integer deleteAddressRelation(CustomerAddressRelationSDO sdo) {
        DeleteRequest deleteRequest = new DeleteRequest();
        deleteRequest.setIds(sdo.getIds());
        if(sdo.getId() != null){
            deleteRequest.setId(Long.valueOf(sdo.getId()));
        }
        deleteRequest.setAddressCode(sdo.getAddressCode());
        deleteRequest.setAddressId(sdo.getAddressId());
        deleteRequest.setRelationId(sdo.getRelationId());
        DeleteResponse deleteResponse = customerAddressRelationService.delete(deleteRequest);
        return deleteResponse.getCount();
    }

    @Override
    public List<CustomerAddressRelationSDO> queryRelationList(CustomerAddressRelationLoadListRequest request) {
        request.setSize(1000);
        request.setStart(0);
        Result<List<CustomerAddressRelation>> result = customerAddressRelationService.loadList(request);
        List<CustomerAddressRelationSDO> list = Nullable.stream(result.getResult()).map(x->{
            CustomerAddressRelationSDO customerAddressRelationSDO = new CustomerAddressRelationSDO();
            BeanUtils.copyProperties(x,customerAddressRelationSDO);
            customerAddressRelationSDO.setId(String.valueOf(x.getId()));
            return customerAddressRelationSDO;
        }).collect(Collectors.toList());
        return list;
    }
}
